package com.lmiot.xyclick.KeYi;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lmiot.xyclick.App.MyApp;
import com.lkx.liandianqinew.R;
import com.lmiot.xyclick.Util.LogUtil;
import com.lmiot.xyclick.Util.PhoneUtil;
import com.lmiot.xyclick.Util.RandomUtil;
import com.lmiot.xyclick.Util.TimeUtils;
import com.lmiot.xyclick.inteface.OnBasicListener;
import com.youyi.yyviewsdklibrary.Dialog.util.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.builder.PostStringBuilder;
import com.zhy.http.okhttp.callback.StringCallback;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.MediaType;

public class HttpUtilXYPro {
    private static final String TAG = "HttpUtilXYPro";
    public static final String URL = "https://www.youyikeji.tech/";
    private static final HttpUtilXYPro ourInstance = new HttpUtilXYPro();
    private static final int pageCount = 6;

    public interface OnSuggeListResult {
        void result(boolean z, String str, List<SuggListBeanRes.DataBean> list);
    }

    public static HttpUtilXYPro getInstance() {
        return ourInstance;
    }

    private HttpUtilXYPro() {
    }

    public void uploadImg(String str, String str2, String str3, final OnBasicListener onBasicListener) {
        String formatFileSize = formatFileSize(str2, 400);
        final String str4 = str + "_changevip";
        BasicPostBean realPostBean = getRealPostBean(new UploadBean(str4, str4 + ".png", str4, "share", "", "changevip", 1, "", "", 0, "", TimeUtils.getCurrentTime(), MyApp.getContext().getString(R.string.app_name), "changevip", PhoneUtil.getAPPVersion(), PhoneUtil.getIMEI(MyApp.getContext()), PhoneUtil.getBrand() + PhoneUtil.getModel(), MyApp.mWidth + "*" + MyApp.mHeight));
        File file = new File(formatFileSize);
        if (file.exists()) {
            ((PostFormBuilder) OkHttpUtils.post().addParams("json", new Gson().toJson(realPostBean)).addFile("full.png", "full.png", file).url("https://www.youyikeji.tech/xypro/api/file/uploadimg")).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    if (onBasicListener != null) {
                        onBasicListener.result(false, "图片上传失败");
                    }
                    LogUtil.d(HttpUtilXYPro.TAG, "上传图片失败：" + e.getMessage());
                }

                @Override
                public void onResponse(String response, int id) {
                    onBasicListener.result(true, PhoneUtil.getIMEI(MyApp.getContext()) + "/" + str4 + ".png");

                }
            });
        } else {
            onBasicListener.result(false, "图片不存在");
        }
    }

    public void delSuggesion(String str, String str2, final OnBasicListener onBasicListener) {
        PhoneUtil.getIMEI(MyApp.getContext());
        ((PostStringBuilder) OkHttpUtils.postString().url("https://www.youyikeji.tech/xypro/api/suggestion/del")).content(AesUtilHttp.set3DesString(new Gson().toJson(getRealPostBean(new DelSuggBean("changevip_" + str))))).mediaType(MediaType.parse("application/json; charset=utf-8")).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                e.printStackTrace();
                if (onBasicListener != null) {
                    onBasicListener.result(false, "");
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (onBasicListener != null) {
                    onBasicListener.result(true, "");
                }
            }
        });
    }

    public void addSuggesion(final String str, final String str2, final String str3, final OnBasicListener onBasicListener) {
        delSuggesion(str, str2, new OnBasicListener() {
            /* class com.lmiot.xyclick.KeYi.HttpUtilXYPro.AnonymousClass3 */

            @Override // com.lmiot.xyclick.inteface.OnBasicListener
            public void result(boolean z, String str) {
                String imei = PhoneUtil.getIMEI(MyApp.getContext());
                ((PostStringBuilder) OkHttpUtils.postString().url("https://www.youyikeji.tech/xypro/api/suggestion/add")).content(AesUtilHttp.set3DesString(new Gson().toJson(HttpUtilXYPro.this.getRealPostBean(new AddSuggession("changevip_" + str, new Gson().toJson(new ChangeVIPData(str, imei, str2, str3)), "changevip", TimeUtils.getCurrentTime(), "连点器", "changevip", PhoneUtil.getAPPVersion(), imei, PhoneUtil.getModel() + "\r" + PhoneUtil.getBrand(), "0"))))).mediaType(MediaType.parse("application/json; charset=utf-8")).build().execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        if (onBasicListener != null) {
                            onBasicListener.result(false, "");
                        }
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (onBasicListener != null) {
                            onBasicListener.result(true, "");
                        }  if (onBasicListener != null) {
                            onBasicListener.result(true, "");
                        }
                    }
                });
            }
        });
    }

    public void suggestionList(final OnSuggeListResult onSuggeListResult) {
        SuggListBean suggListBean = new SuggListBean("changevip", PhoneUtil.getIMEI(MyApp.getContext()));
        String json = new Gson().toJson(getRealPostBean(suggListBean));
        LogUtil.d(TAG, "查询建议：" + new Gson().toJson(suggListBean));
        ((PostStringBuilder) OkHttpUtils.postString().url("https://www.youyikeji.tech/xypro/api/suggestion/searchlistbyuser")).content(AesUtilHttp.set3DesString(json)).mediaType(MediaType.parse("application/json; charset=utf-8")).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (onSuggeListResult != null) {
                    onSuggeListResult.result(false, "网络错误!", null);
                }
                e.printStackTrace();
            }

            @Override
            public void onResponse(String str, int id) {
                LogUtil.d(HttpUtilXYPro.TAG, "查询建议返回：" + str);
                try {
                    List<SuggListBeanRes.DataBean> data = ((SuggListBeanRes) new Gson().fromJson(str, SuggListBeanRes.class)).getData();
                    if (data == null) {
                        data = new ArrayList<>();
                    }
                    if (onSuggeListResult != null) {
                        onSuggeListResult.result(true, "请求成功！", data);
                    }
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                    OnSuggeListResult onSuggeListResult2 = onSuggeListResult;
                    if (onSuggeListResult2 != null) {
                        onSuggeListResult2.result(false, "数据错误", null);
                    }
                }
            }
        });
    }

    public static String formatFileSize(String str, int i) {
        File file = new File(str);
        return file.length() > ((long) (i * 1024)) ? lessBitmap(BitmapFactory.decodeFile(str), file, i) : str;
    }

    public static String lessBitmap(Bitmap bitmap, File file, int i) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
            int i2 = 95;
            while (byteArrayOutputStream.toByteArray().length / 1024 > i) {
                byteArrayOutputStream.reset();
                bitmap.compress(Bitmap.CompressFormat.JPEG, i2, byteArrayOutputStream);
                i2 -= 5;
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            byte[] bArr = new byte[10];
            ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
            while (true) {
                int read = byteArrayInputStream.read(bArr);
                if (read != -1) {
                    byteArrayOutputStream2.write(bArr, 0, read);
                } else {
                    fileOutputStream.write(byteArrayOutputStream2.toByteArray());
                    byteArrayOutputStream2.close();
                    byteArrayInputStream.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    return file.getAbsolutePath();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return file.getAbsolutePath();
        }
    }

    public void zfbPay(String str, String str2, String str3, String str4, String str5, final OnBasicListener onBasicListener) {
        String createZfbPayID = TimeUtils.createZfbPayID();
        String imei = PhoneUtil.getIMEI(MyApp.getContext());
        String string = MyApp.getContext().getString(R.string.app_name);
        String packageName = MyApp.getContext().getPackageName();
        String json = new Gson().toJson(getRealPostBean(new NewPayBean(createZfbPayID, str3, str3, "zfb", imei, string, packageName, str2, str, "会员等级" + str, TimeUtils.getCurrentTime(), str4, str5)));
        LogUtil.d(TAG, "支付宝付款：" + json);
        ((PostStringBuilder) OkHttpUtils.postString().url("https://www.youyikeji.tech/xypro/api/order/pay")).content(AesUtilHttp.set3DesString(json)).mediaType(MediaType.parse("application/json; charset=utf-8")).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception exc, int id) {
                LogUtil.d(HttpUtilXYPro.TAG, "支付宝付款失败：" + exc.getMessage());
                ToastUtil.err("支付宝付款失败：" + exc.getMessage());
                exc.printStackTrace();
            }

            @Override
            public void onResponse(String response, int id) {
                if (!TextUtils.isEmpty(str)) {
                    LogUtil.d(HttpUtilXYPro.TAG, "本地付款成功：" + str);
                    AliPayResBean aliPayResBean = (AliPayResBean) new Gson().fromJson(str, AliPayResBean.class);
                    if (aliPayResBean.getErrcode() == 1) {
                        String data = aliPayResBean.getData();
                        if (onBasicListener != null) {
                            onBasicListener.result(true, data);
                            return;
                        }
                        return;
                    }
                    ToastUtil.err("支付失败：" + aliPayResBean.getErrmsg());
                    OnBasicListener onBasicListener2 = onBasicListener;
                    if (onBasicListener2 != null) {
                        onBasicListener2.result(false, aliPayResBean.getErrmsg());
                    }
                }
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private BasicPostBean getRealPostBean(Object obj) {
        BasicPostBean basicPostBean = new BasicPostBean();
        BasicPostBean.HeadBean headBean = new BasicPostBean.HeadBean();
        String randomString = RandomUtil.getRandomString(32);
        String str = System.currentTimeMillis() + "";
        headBean.setCmd(1001);
        String sign = AesUtilHttp.getSign(1001, randomString, str);
        headBean.setCmd(1001);
        headBean.setNoncestr(randomString);
        headBean.setTimestamp(str);
        headBean.setSign(sign);
        basicPostBean.setHead(headBean);
        BasicPostBean.BodyBean bodyBean = new BasicPostBean.BodyBean();
        bodyBean.setErrcode(1);
        bodyBean.setErrmsg("");
        bodyBean.setData(obj);
        basicPostBean.setBody(bodyBean);
        return basicPostBean;
    }

    public static <T> T getRealResBean(String str, Class<T> cls) {
        try {
            return (T) new Gson().fromJson(new Gson().toJson(((BasicResBean) new Gson().fromJson(str, (Class) BasicResBean.class)).getData()), (Class) cls);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
